package com.weavernorth.performance;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.workflow.webservices.*;

public class ManagerServerFlow {

    private static Log log = org.apache.commons.logging.LogFactory.getLog(ManagerServerFlow.class.getName());

    //创建店长  服务标准流程
    public void createRetail() {
        RecordSet rsShop = new RecordSet();
        RecordSet rsShopTwo = new RecordSet();
        RecordSet rsGrade = new RecordSet();
        RecordSet rsClerk = new RecordSet();
        RecordSet rsClerkNum = new RecordSet();
        RecordSet rsDc = new RecordSet();
        RecordSet rsPfr = new RecordSet();
        //查询店铺信息表获取数据
        String strShopsql = "select qyjl,fgs  from uf_dpxx  where ( sfdpphd=1  )  group by qyjl,fgs  ";
        log.info("获取店铺信息的sql:" + strShopsql);
        rsShop.executeQuery(strShopsql);
        while (rsShop.next()) {

            //获取分公司的值
            String strFgs = Util.null2String(rsShop.getString("fgs"));
            log.info("分公司的值：" + strFgs);
            //获取区域经理
            String strLsjl = Util.null2String(rsShop.getString("qyjl"));
            log.info("区域经理的值：" + strLsjl);
//            //获取店铺信息其他数据的sql
//            String strShopSqlTwo="select dc,ppkdx,dpzt,dpjb,dp from uf_dpxx where  ( sfdpphd=1  ) and  fgs='"+strFgs+"' and qyjl='"+strLsjl+"'";
//            log.info("获取店铺信息其他数据的sql："+strShopSqlTwo);
//            rsShopTwo.executeQuery(strShopSqlTwo);

            //创建流程的主表
            //存放主表字段的数组
            WorkflowRequestTableField[] wrti = new WorkflowRequestTableField[2];
            //添加区域经理字段
            log.info("区域经理：" + strLsjl);
            wrti[0] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[0].setFieldName("qyjl");
            //赋值
            wrti[0].setFieldValue(Util.null2String(strLsjl));
            wrti[0].setView(true);
            wrti[0].setEdit(true);
            wrti[0].setFieldOrder(0);
            wrti[0].setMand(false);
            //添加分公司字段
            log.info("分公司：" + strFgs);
            wrti[1] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[1].setFieldName("fgs");
            //赋值
            wrti[1].setFieldValue(Util.null2String(strFgs));
            wrti[1].setView(true);
            wrti[1].setEdit(true);
            wrti[1].setFieldOrder(0);
            wrti[1].setMand(false);


            WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[1];
            wrtri[0] = new WorkflowRequestTableRecord();
            wrtri[0].setWorkflowRequestTableFields(wrti);
            WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
            wmi.setRequestRecords(wrtri);


            //通过分公司值获取对应店长服务标准流程的workflowid的sql
            String strGradeSql = "select dckhlcfwbz from uf_khpflcpz where fgs='" + strFgs + "'";
            log.info("获取workflowid的sql：" + strGradeSql);
            rsGrade.executeQuery(strGradeSql);
            //判断是否有对应workflowid
            String strGradeWfId = "";
            if (rsGrade.next()) {
                strGradeWfId = Util.null2String(rsGrade.getString("dckhlcfwbz"));
                log.info("店长服务标准流程的workflowid：" + strGradeWfId);
            }
            //获取店铺信息表中店长的数据的sql
            String strDcNameSql = "select dc,ppkdx,dpzt,dpjb,dp  from uf_dpxx where ( sfdpphd=1  ) and fgs='" + strFgs + "' and qyjl='" + strLsjl + "' and dc is not null";
            log.info("获取店铺信息表中店长的数据的sql：" + strDcNameSql);
            rsDc.executeQuery(strDcNameSql);

            //获取明细表长度的sql
//            String strClerkNumSql = "select count(dc) as num  from uf_dpxx where ( sfdpphd=1  ) and  fgs='" + strFgs + "' and qyjl='" + strLsjl + "'";
//            log.info("获取明细表长度的sql：" + strClerkNumSql);
//            rsClerkNum.executeQuery(strClerkNumSql);
//            //明细表长度
//            int intDetailNum = 0;
//            if (rsClerkNum.next()) {
//                intDetailNum = Integer.parseInt(Util.null2String(rsClerkNum.getString("num")));
//                log.info("明细表的长度：" + intDetailNum);
//            }
            //创建流程明细表

            //循环参数
            int intNum = 0;
            //根据店铺级别封装数据,A,A+类型
            JSONArray jsonArrA = new JSONArray();
            //B,C,D类
            JSONArray jsonArrBCD = new JSONArray();
            while (rsDc.next()) {
                JSONObject jsonObject = new JSONObject();

                //店铺状态
                String strStauts = "";
                //店铺名称
                String strDpName = "";

                //店铺级别
                String strRank = "";
                //品牌
                String strBrand = "";

               //分公司
                jsonObject.put("fgs", strFgs);
                //店长名称
                String strDcName = Util.null2String(rsDc.getString("dc"));
                jsonObject.put("dc", strDcName);
                //获取店铺级别
                strRank = Util.null2String(rsDc.getString("dpjb"));
                jsonObject.put("dpjb", strRank);
                log.info("店铺级别：" + strRank);
                //品牌
                strBrand = Util.null2String(rsDc.getString("ppkdx"));
                jsonObject.put("brand", strBrand);
                log.info("品牌：" + strBrand);
                //获取店铺状态
                strStauts = Util.null2String(rsDc.getString("dpzt"));
                jsonObject.put("dpzt", strStauts);
                log.info("店铺状态：" + strStauts);

                //店铺名称
                strDpName = Util.null2String(rsDc.getString("dp"));
                jsonObject.put("dp", strDpName);
                log.info("店铺名称：" + strDpName);
                //获取明细表数据的sql
                String strClerkSql = "select id , jobtitle,workcode from hrmresource where  id= '" + strDcName + "'";
                log.info("获取明细数据的sql:" + strClerkSql);
                rsClerk.executeQuery(strClerkSql);
                if (rsClerk.next()) {
                    //获取店长姓名
                    String strClerkName = Util.null2String(rsClerk.getString("id"));
                    //获取店长工号
                    String strClerkCode = Util.null2String(rsClerk.getString("workcode"));
                    jsonObject.put("workcode", strClerkCode);
                    //获取店长岗位
                    String strClerkJob = Util.null2String(rsClerk.getString("jobtitle"));
                    jsonObject.put("jobtitle", strClerkJob);

                }


                //根据店铺级别放入数组里
                if ("0".equals(strRank) || "1".equals(strRank)) {
                    jsonArrA.add(jsonObject);
                } else {
                    jsonArrBCD.add(jsonObject);
                }
            }


            //基本信息
            WorkflowBaseInfo wbi = new WorkflowBaseInfo();
            //流程的workflowid
            wbi.setWorkflowId(strGradeWfId);


            WorkflowRequestInfo wri = new WorkflowRequestInfo();


            //紧急程度
            wri.setRequestLevel("0");
            //请求的名称
            wri.setRequestName("店长服务标准考核评分流程");
            //停留在提交节点
            wri.setIsnextflow("0");

            //主表数据
            wri.setWorkflowMainTableInfo(wmi);
            wri.setWorkflowBaseInfo(wbi);

            //创建人
            String strCreater = "";
            //创建人根据品牌和级别对应的区域经理或者时尚学院
            //0和1对应级别A和A+
            if (jsonArrA != null && jsonArrA.size() > 0) {
                //明细表个数为1
                WorkflowDetailTableInfo wdti[] = new WorkflowDetailTableInfo[1];
                //限定明细表的长度
                wrtri = new WorkflowRequestTableRecord[jsonArrA.size()];
                wdti = getDetail(jsonArrA, wdti, wrtri);

                //查询评分人的sql
                String strNameSql = "select  pfr from uf_khpflcpfrpz  where dppp='" + (jsonArrA.getJSONObject(0).getString("brand")) + "' and dpjb='" + (jsonArrA.getJSONObject(0).getString("dpjb")) + "'  and  pflx='1'";
                log.info("查询A或A+评分人的sql：" + strNameSql);
                rsPfr.executeQuery(strNameSql);
                if (rsPfr.next()) {
                    String strPfr = Util.null2String(rsPfr.getString("pfr"));
                    strCreater = strPfr;
                    log.info("A或A+级别的店铺对应评分人：" + strPfr);
                    wri.setCreatorId(strPfr);
                }


                //明细表数据
                wri.setWorkflowDetailTableInfos(wdti);
                WorkflowServiceImpl wfsi = new WorkflowServiceImpl();

                String strRequestid = wfsi.doCreateWorkflowRequest(wri, Integer.parseInt(strCreater));
                log.info("生成流程的requestid:" + strRequestid);

            }


            if (jsonArrBCD != null && jsonArrBCD.size() > 0) {
                //明细表个数为1
                WorkflowDetailTableInfo wdti[] = new WorkflowDetailTableInfo[1];
                //限定明细表的长度
                wrtri = new WorkflowRequestTableRecord[jsonArrBCD.size()];
                wdti = getDetail(jsonArrBCD, wdti, wrtri);

                strCreater = strLsjl;
                wri.setCreatorId(strLsjl);
                //明细表数据
                wri.setWorkflowDetailTableInfos(wdti);
                WorkflowServiceImpl wfsi = new WorkflowServiceImpl();

                String strRequestid = wfsi.doCreateWorkflowRequest(wri, Integer.parseInt(strCreater));
                log.info("生成流程的requestid:" + strRequestid);
            }


        }


    }


    /**
     * 封装明细数据
     */

    public WorkflowDetailTableInfo[] getDetail(JSONArray jsonArray, WorkflowDetailTableInfo[] wdti, WorkflowRequestTableRecord[] wrtri) {

        for (int i = 0; i < jsonArray.size(); i++) {

            JSONObject jsonObject =jsonArray.getJSONObject(i);
            log.info("封装明细json:"+jsonObject);

            //有8个字段长度为8
            WorkflowRequestTableField[] wrti = new WorkflowRequestTableField[8];

            //添加店长姓名字段

            wrti[0] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[0].setFieldName("xm");
            //赋值
            wrti[0].setFieldValue(jsonObject.getString("dc"));
            wrti[0].setView(true);
            wrti[0].setEdit(true);

            //添加店长工号字段

            wrti[1] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[1].setFieldName("gh");
            //赋值
            wrti[1].setFieldValue(jsonObject.getString("workcode"));
            wrti[1].setView(true);
            wrti[1].setEdit(true);

            //添加店长岗位字段

            wrti[2] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[2].setFieldName("zw");
            //赋值
            wrti[2].setFieldValue(jsonObject.getString("jobtitle"));
            wrti[2].setView(true);
            wrti[2].setEdit(true);

            //添加店长分公司字段

            wrti[3] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[3].setFieldName("fgs");
            //赋值
            wrti[3].setFieldValue(jsonObject.getString("fgs"));
            wrti[3].setView(true);
            wrti[3].setEdit(true);

            //添加店长店铺名称字段

            wrti[4] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[4].setFieldName("dpmc");
            //赋值
            wrti[4].setFieldValue(jsonObject.getString("dp"));
            wrti[4].setView(true);
            wrti[4].setEdit(true);

            //添加店长级别字段

            wrti[5] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[5].setFieldName("dpjb");
            //赋值
            wrti[5].setFieldValue(jsonObject.getString("dpjb"));
            wrti[5].setView(true);
            wrti[5].setEdit(true);

            //添加店长状态字段

            wrti[6] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[6].setFieldName("dpzt");
            //赋值
            wrti[6].setFieldValue(jsonObject.getString("dpzt"));
            wrti[6].setView(true);
            wrti[6].setEdit(true);

            //添加品牌字段

            wrti[7] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[7].setFieldName("pp");
            //赋值
            wrti[7].setFieldValue(jsonObject.getString("brand"));
            wrti[7].setView(true);
            wrti[7].setEdit(true);


            wrtri[i] = new WorkflowRequestTableRecord();
            wrtri[i].setWorkflowRequestTableFields(wrti);

        }
        wdti[0] = new WorkflowDetailTableInfo();
        wdti[0].setWorkflowRequestTableRecords(wrtri);
        return wdti;

    }


}
